package com.wandian.texas.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.CallableStatement;

import com.wandian.util.Log;

/**
 * @author long
 * @Create 2012-10-19 下午2:36:21
 * @QQ 108232706
 * @version 1.0
 * @description Result
 */
public class Result {
	public boolean IsSuccess = false;
	public String Msg;
	public ResultSet ResultSet;
	public PageInfo pageInfo;
	public int RowSize;
	public Object[][] DataRows;
	public boolean hasResultSet;
	public CallableStatement stmt;
	public Object dataList;

	public Result() {

	}

	public void clear() {
		IsSuccess = false;
		hasResultSet = false;
		stmt = null;
		ResultSet = null;
		// RowSize = 0;
		// DataRows = null;
	}

	public String getRowsJson() {
		StringBuilder sbRows = new StringBuilder("{");
		if (this.RowSize > 0 && DataRows != null) {
			for (int i = 0; i < DataRows.length; i++) {
				if (i != 0)
					sbRows.append(",");
				sbRows.append("{" + i + ":{");
				for (int j = 0; j < DataRows[i].length; j++) {
					if (j != 0)
						sbRows.append(",");
					sbRows.append(j + ":\"" + DataRows[i][j] + "\"");
				}
				sbRows.append("}}");
			}
		}
		sbRows.append("}");
		return sbRows.toString();
	}

	public void prepareAddBatch(Object... parms) throws SQLException {
		if ((stmt == null) || (parms == null))
			return;
		for (int i = 1; i <= parms.length; i++) {
			stmt.setObject(i, parms[i - 1]);
		}
		stmt.addBatch();
	}
	public String getRows() {
		StringBuilder sbRows = new StringBuilder("[");
		if (this.RowSize > 0 && DataRows != null) {
			for (int i = 0; i < DataRows.length; i++) {
				if (i != 0)
					sbRows.append(",");
				sbRows.append("[");
				for (int j = 0; j < DataRows[i].length; j++) {
					if (j != 0)
						sbRows.append(",");
					sbRows.append("\"" + DataRows[i][j] + "\"");
				}
				sbRows.append("]");
			}
		}
		sbRows.append("]");
		return sbRows.toString();
	}

	public String toString() {
		if (IsSuccess)
			return String.format("成功,%s,pageInfo:%s", Msg, pageInfo == null ? "" : pageInfo);
		else {
			return String.format("失败,%s", Msg);
		}
	}

	public void run() {
		Log.debug("run Result command");
	}

}
